//
//  RABookDetailCenterCell.m
//  ReadApp
//
//  Created by 飞猪 on 2018/11/8.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RABookDetailCenterCell.h"
#import "RABookDetailModel.h"

@interface RABookDetailCenterCell ()

@property (nonatomic, strong) UILabel *contentLab;

@property (nonatomic, strong) UIButton *morebtn;

@property (nonatomic, strong) UIView *btmLine;

@end

@implementation RABookDetailCenterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    [self addSubViews];
  }
  return self;
}

- (void)addSubViews {
  
  self.backgroundColor = [UIColor whiteColor];
  self.contentView.backgroundColor = [UIColor whiteColor];
  self.selectionStyle = UITableViewCellSelectionStyleNone;
  
  [self.contentView addSubview:self.contentLab];
  [self.contentView addSubview:self.morebtn];
  [self.contentView addSubview:self.btmLine];
  
  [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.right.mas_equalTo(self.contentView).mas_equalTo(UIEdgeInsetsMake(10, 10, 10, 10));
  }];
  
  [self.morebtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.mas_equalTo(self.contentView);
    make.top.mas_equalTo(self.contentLab.mas_bottom);
    make.bottom.mas_equalTo(self.btmLine.mas_top);
    make.height.mas_equalTo(32.f);
  }];
  
  [self.btmLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.left.bottom.mas_equalTo(0);
    make.height.mas_equalTo(0.5);
  }];
  
}

- (void)setModel:(RABookDetailModel *)model {
  _model = model;
  
  self.contentLab.attributedText = [self setLineSpace:4.f withLabelText:_model.bookIntro withFont:[UIFont systemFontOfSize:13.f] withZspace:@0];
  
  if (_model.isExpand) {
    self.morebtn.selected = YES;
    self.contentLab.numberOfLines = 0;
    
    CGFloat newHeight = [self getSpaceLabelHeight:_model.bookIntro withFont:[UIFont systemFontOfSize:13.f] withWidth:kScreenWidth - 20 withSpace:4.f withZspace:@0];
    [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
      make.height.mas_equalTo(newHeight);
    }];
  }
  else {
    self.morebtn.selected = NO;
    self.contentLab.numberOfLines = 3;
     [self.contentLab sizeToFit];
  }
  
}


#pragma mark - Event
- (void)clicked:(UIButton *)btn {
  btn.selected = !btn.isSelected;
  
  if (self.moreClick) {
    self.moreClick();
  }
}

- (UILabel *)contentLab {
  if (!_contentLab) {
    _contentLab = [UILabel new];
    _contentLab.font = [UIFont systemFontOfSize:13.f];
    _contentLab.textColor = [UIColor lightGrayColor];
    _contentLab.numberOfLines = 3;
    
    //    _titleLab.text = @"本期热推";
  }
  return _contentLab;
}

- (UIButton *)morebtn {
  if (!_morebtn) {
    _morebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_morebtn setImage:[UIImage imageNamed:@"bottom_arrow"] forState:UIControlStateNormal];
    [_morebtn setImage:[UIImage imageNamed:@"top_arrow"] forState:UIControlStateSelected];
    [_morebtn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _morebtn;
}
  
- (UIView *)btmLine {
  if (!_btmLine) {
    _btmLine = [[UIView alloc] init];
    _btmLine.backgroundColor = UIColorHex(ebebeb);
  }
  return _btmLine;
}

/**
 *  给UILabel设置行间距和字间距
 *  @param space 间距
 *  @param text  内容
 *  @param font  字体
 *  @param zpace  字间距 --> @10 这样设置  默认的话设置 0 就ok
 */
- (NSAttributedString *)setLineSpace:(CGFloat)space withLabelText:(NSString*)text withFont:(UIFont*)font withZspace:(NSNumber *)zpace {
  
  NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
  
  paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
  paraStyle.alignment = NSTextAlignmentLeft;
  paraStyle.lineSpacing = space; //设置行间距
  paraStyle.hyphenationFactor = 1.0;
  paraStyle.firstLineHeadIndent =0.0;
  paraStyle.paragraphSpacingBefore =0.0;
  paraStyle.headIndent = 0;
  paraStyle.tailIndent = 0;
  // 设置字间距 NSKernAttributeName:@1.5f
  NSDictionary *dic;
  if (zpace == 0) {
    dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
           };
  }else {
    dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:zpace
           };
  }
  
  return [[NSAttributedString alloc] initWithString:text attributes:dic];
  
}


/**
 *  计算UILabel的高度(带有行间距的情况)
 *  @param text  内容
 *  @param font  字体
 *  @param width 宽度
 *  @return 高度
 *  @param zpace  字间距 --> @10 这样设置  默认的话设置 0 就ok
 */
- (CGFloat)getSpaceLabelHeight:(NSString*)text withFont:(UIFont*)font withWidth:(CGFloat)width withSpace:(CGFloat)space withZspace:(NSNumber *)zpace {
  
  NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
  paraStyle.lineBreakMode =NSLineBreakByCharWrapping;
  paraStyle.alignment =NSTextAlignmentLeft;
  paraStyle.lineSpacing = space;
  paraStyle.hyphenationFactor = 1.0;
  paraStyle.firstLineHeadIndent =0.0;
  paraStyle.paragraphSpacingBefore =0.0;
  paraStyle.headIndent = 0;
  paraStyle.tailIndent = 0;
  
  // 设置字间距 NSKernAttributeName:@1.5f
  NSDictionary *dic;
  if (zpace == 0) {
    dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
           };
  }else {
    dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:zpace
           };
  }
  
  CGSize size = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
  
  return size.height;
  
}



@end
